Streamlit for Data Science by Tyler Richards

Streamlit for Data Science by Tyler Richards

Author:Tyler Richards
Language: eng
Format: epub
Tags: COM060150 - COMPUTERS / Web / User Generated Content, COM021040 - COMPUTERS / Databases / Data Warehousing, COM051460 - COMPUTERS / Programming / Mobile Devices
Publisher: Packt
Published: 2023-09-22T03:51:24+00:00


Picking colors with a color picker

Colors are very difficult to take in as user input in apps. If a user wants red, do they want light red or dark red? Maroon or a pinkish red? Streamlit’s approach to this problem is st.color_picker(), which lets the user pick a color as a part of their user input, and returns that color in a hex string (which is a unique string that defines very specific color shades used by most graphing libraries as input). The following code adds this color picker to our previous app and changes the color of the Seaborn graphs to be based on the color that the user selects:

import pandas as pd import plotly.express as px import streamlit as st st.set_page_config(layout="wide") st.title("SF Trees") st.write( """ This app analyses trees in San Francisco using a dataset kindly provided by SF DPW. The dataset is filtered by the owner of the tree as selected in the sidebar! """ ) trees_df = pd.read_csv("trees.csv") today = pd.to_datetime("today") trees_df["date"] = pd.to_datetime(trees_df["date"]) trees_df["age"] = (today - trees_df["date"]).dt.days unique_caretakers = trees_df["caretaker"].unique() owners = st.sidebar.multiselect("Tree Owner Filter", unique_caretakers) graph_color = st.sidebar.color_picker("Graph Colors") if owners: trees_df = trees_df[trees_df["caretaker"].isin(owners)]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.